package com.code.socket3;

import java.io.OutputStream;
import java.net.Socket;

/**
 * @description:
 * @className: SocketClient
 * @package: com.code.socket3
 * @author: yangzhi@hsyuntai.com
 * @date: 2019/10/16 19:23
 * @copyright: 版权归 HSYUNTAI 所有
 */
public class SocketClient {

    static String host = "127.0.0.1";
    static int port = 55533;

    public static void main(String args[]) throws Exception {

        // 与服务端建立连接
        Socket socket = new Socket(host, port);
        // 建立连接后获得输出流
        OutputStream outputStream = socket.getOutputStream();
        String message = "你好  java";
        //首先需要计算得知消息的长度
        byte[] sendBytes = message.getBytes("UTF-8");
        //然后将消息的长度优先发送出去
        outputStream.write(sendBytes.length >> 8);
        outputStream.write(sendBytes.length);
        //然后将消息再次发送出去
        outputStream.write(sendBytes);
        outputStream.flush();
        //==========此处重复发送一次，实际项目中为多个命名，此处只为展示用法
        message = "第二条消息";
        sendBytes = message.getBytes("UTF-8");
        outputStream.write(sendBytes.length >> 8);
        outputStream.write(sendBytes.length);
        outputStream.write(sendBytes);
        outputStream.flush();
        //==========此处重复发送一次，实际项目中为多个命名，此处只为展示用法
        message = "第三条消息";
        sendBytes = message.getBytes("UTF-8");
        outputStream.write(sendBytes.length >> 8);
        outputStream.write(sendBytes.length);
        outputStream.write(sendBytes);

        outputStream.close();
        socket.close();
    }

    public void a() {
        try {
            Socket socket = new Socket(host, port);
            socket.isInputShutdown();
            socket.isInputShutdown();


            socket.isConnected();
        } catch (Exception e) {

        }
    }
}
